// Copyright (c) XiGuan 2023
// Author： xiguan
// Email: xiguan.teng@qq.com
// Create on 2023/11/7
// TODO:
//

#ifndef WEBSERVER_POOLS_SQLCONNRAII_H
#define WEBSERVER_POOLS_SQLCONNRAII_H

#include <SqlConnPool.h>
#include <assert.h>

class SqlConnRAII {
 private:
  MYSQL *sql_;
  SqlConnPool *sql_conn_pool_;

 public:
  SqlConnRAII(MYSQL **_sql, SqlConnPool *_sql_conn_pool) {
    assert(_sql_conn_pool);
    *_sql = _sql_conn_pool->GetConn();
    sql_ = *_sql;
    sql_conn_pool_ = _sql_conn_pool;
  }

  ~SqlConnRAII() {
    if (sql_) {
      sql_conn_pool_->free_conn(sql_);
    }
  }
};

#endif  // WEBSERVER_POOLS_SQLCONNRAII_H
